﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;
using Meqs.Utilities;

public partial class Credit_Teacher_Reprots_StudentMoralFile : ContainerBasePage
{
    protected static int StuID
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["id"], out id))
                return id;
            return -1;
        }
    }
    protected static String StuCode
    {
        get
        {
            String code;
            code = HttpContext.Current.Request["code"];
            return code;
        }
    }

    protected static String StuName
    {
        get
        {
            String name;
            name = HttpUtility.UrlDecode(HttpContext.Current.Request["name"]);
            return name;
        }
    }

    protected static int rYear
    {
        get
        {
            int rYear;
            if (int.TryParse(HttpContext.Current.Request["rYear"], out rYear))
                return rYear;
            return -1;
        }
    }

    protected static int rTerm
    {
        get
        {
            int rTerm;
            if (int.TryParse(HttpContext.Current.Request["rTerm"], out rTerm))
                return rTerm;
            return -1;
        }
    }

    private CauseFormula causeFormula;
    private CauseLevelTool causeLevelTool;

    protected IList<CreditHistory> creditList = null;
    protected IList<StudentHistory> historyList = null;
    private IList<StudentMoralityScoreExView> moralityList = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            btnBind_Click(btnBind, new EventArgs());
    }

    protected void btnBind_Click(object sender, EventArgs e)
    {
        IList<ClsInfo> l = new List<ClsInfo>();
        l.Add(new ClsInfo());
        Repeater1.DataSource = l;
        Repeater1.DataBind();
        //WriteContext();
    }

    protected string WriteContext()
    {
        StringBuilder sb = new StringBuilder();
        StudentInfo si = Container<IStudentInfoLogic>.Instance.FindById(StuID);

        IMoralityTypeLogic mtLogic = Container<IMoralityTypeLogic>.Instance;
        IMoralityItemLogic miLogic = Container<IMoralityItemLogic>.Instance;

        IList<MoralityType> list = mtLogic.GetAllMoralityType(SchoolMark, rYear, rTerm);

        IList<MoralStuScore> scores = Container<IMoralityDetailLogic>.Instance.GetStuMoralScore("Student_Month_Action", si.RID, rYear,rTerm,SchoolMark);
        String title2 = "";
        title2 = "<tr>";


        sb.Append("<h2 style='text-align:center;'>" + rYear + " - " + (rYear + 1) +" " + (rTerm==1?"上学期":"下学期") + " 罗芳中学学生德育学分记录表</h2><br/>");
        sb.Append(String.Format("学号:{0} 姓名:{1} 序号:{2} <br/>", si.StudentCode, si.StudentName, si.ClsIndex+1));
        sb.Append("<table  class=\"useList1\">");
        sb.Append("<tr>");
        sb.Append("<th style='text-align:left;padding-left:2px;' rowspan=2>月份</th>");
       // sb.Append("<td style='text-align:left;padding-left:2px;' rowspan=2>周次</td>");
        IList<MoralityItem> alltiem = new List<MoralityItem>();
        foreach(MoralityType m in list)
        {
            IList<MoralityItem> miList = miLogic.GetItemByType(SchoolMark, m.RID);
            sb.Append("<th style='text-align:left;padding-left:2px;' colspan=" + miList.Count + ">" + m.Caption + "</th>");
            foreach (MoralityItem mi in miList)
            {
                alltiem.Add(mi);
                title2 += "<th style='text-align:left;padding-left:2px;'>" + mi.Caption + "</th>";
            }
        }
        //sb.Append("<td style='text-align:left;padding-left:2px;' rowspan=2>周小结</td>");
        sb.Append("<th style='text-align:left;padding-left:2px;' rowspan=2>月小结</th>");
        sb.Append("</tr>");

        title2 += "</tr>";
        sb.Append(title2);


        int weeks = 0;
        int month = 0;
        SchoolCommon.GetWeekOfTerm(rYear, rTerm, SchoolMark, getEndTime(), out weeks);
        for (int state = 0; state <= weeks; state++)
        {
            DateTime timeStart = new DateTime();//开始时间
            DateTime tiemEnd = new DateTime();
            SchoolCommon.GetWeekInTerm(rYear, rTerm, SchoolMark, (state + 1), out timeStart, out tiemEnd);
            if (month != timeStart.Month)
            {
                month = timeStart.Month;
                sb.Append("<tr>");
                sb.Append("<td style='text-align:left;padding-left:2px;' >" + timeStart.Month + "</td>");
                foreach (MoralityItem mi in alltiem)
                {
                    bool isdo = false;
                    foreach (MoralStuScore s in scores)
                    {
                        DateTime stime = (DateTime)s.StartTime;
                        if (mi.RID == s.ItemID && stime.Year == timeStart.Year && stime.Month == timeStart.Month )
                        {
                            isdo = true;
                            sb.Append("<td style='text-align:left;padding-left:2px;' >" + (s.Param > 100 ? 100 : s.Param).ToString() + "</td>");
                            break;
                        }
                    }
                    //Object o = getItemParam(mi.RID, state, scores);
                    if (!isdo)
                        sb.Append("<td style='text-align:left;padding-left:2px;' >" + "" + "</td>");
                }
                Object mo = getMonthScore(timeStart, scores);
                sb.Append("<td style='text-align:left;padding-left:2px;'>" + (mo == null ? "" : mo.ToString()) + "</td>");
            }

        }
        sb.Append("<tr>");
        sb.Append("<td style='text-align:left;padding-left:2px;' >小结</td>");
        foreach (MoralityItem mi in alltiem)
        {
            Object o = getItemParam(mi.RID, scores);
            sb.Append("<td style='text-align:left;padding-left:2px;' >" + (o == null ? "" : o.ToString()) + "</td>");
        }
        Object ao = getScore(scores);

        sb.Append("<td style='text-align:left;padding-left:2px;' >" + (ao == null ? "" : ao.ToString()) + "</td>");

        sb.Append("</tr>");



        //for (int state = 0; state <= weeks; state++)
        //{
        //    DateTime timeStart = new DateTime();//开始时间
        //    DateTime tiemEnd = new DateTime();
        //    SchoolCommon.GetWeekInTerm(rYear, rTerm, SchoolMark, (state + 1), out timeStart, out tiemEnd);
        //    int rowspan = 0;
        //    if (month != timeStart.Month)
        //    {
        //        rowspan = getRolspan(weeks,state, timeStart.Month);
        //        month = timeStart.Month;
        //    }
        //    sb.Append("<tr>");
        //    if (rowspan > 0)
        //    {
        //        sb.Append("<td style='text-align:left;padding-left:2px;' rowspan=" + rowspan + " >" + timeStart.Month + "</td>");
                
        //    }
        //    sb.Append("<td style='text-align:left;padding-left:2px;' >" + (state + 1) + "</td>");
        //    foreach (MoralityItem mi in alltiem)
        //    {
        //        Object o = getItemParam(mi.RID, state, scores);
        //        sb.Append("<td style='text-align:left;padding-left:2px;' >" + (o==null?"":o.ToString()) + "</td>");
        //    }
        //    Object wo = getWeekScore( state, scores);
        //    sb.Append("<td style='text-align:left;padding-left:2px;' >" + (wo == null ? "" : wo.ToString()) + "</td>");
        //    if (rowspan > 0)
        //    {
        //        Object mo = getMonthScore(timeStart, scores);
        //        sb.Append("<td style='text-align:left;padding-left:2px;'  rowspan=" + rowspan + ">" + (mo == null ? "" : mo.ToString()) + "</td>");
        //    }
        //    sb.Append("</tr>");
        //}

        //sb.Append("<tr>");
        //sb.Append("<td style='text-align:left;padding-left:2px;' colspan=2 >小结</td>");
        //foreach (MoralityItem mi in alltiem)
        //{
        //    Object o = getItemParam(mi.RID,  scores);
        //    sb.Append("<td style='text-align:left;padding-left:2px;' >" + (o == null ? "" : o.ToString()) + "</td>");
        //}
        //Object ao = getScore( scores);
        //sb.Append("<td style='text-align:left;padding-left:2px;' >" + (ao == null ? "" : ao.ToString()) + "</td>");
        //sb.Append("<td style='text-align:left;padding-left:2px;' >" + (ao == null ? "" : ao.ToString()) + "</td>");

        //sb.Append("</tr>");
        //sb.Append(String.Format("<tr><td style='text-align:left;padding-left:2px;'>学号:{0} 姓名:{1} 序号:{2}</td>", si.StudentCode, si.StudentName, si.ClsIndex));
        sb.Append("</table>");
        return sb.ToString();
    }
    
    private Object getItemParam(int itemid, IList<MoralStuScore> scores)
    {
        double score = 0;
        int num = 0;
        foreach (MoralStuScore s in scores)
        {
            DateTime stime = (DateTime)s.StartTime;
            if (itemid == s.ItemID )
            {
                score +=(double) s.Param;
                num++;
            }
        }
        if (num > 0)
            return Math.Round(score / num, 2);
        return 0;
    }
    private Object getScore( IList<MoralStuScore> scores)
    {
        double score = 0;
        IList<int> month = new List<int>();
        foreach (MoralStuScore s in scores)
        {
            DateTime stime = (DateTime)s.StartTime;
            if (!month.Contains(stime.Month))
            {
                month.Add(stime.Month);
            }
            score += (double)(s.Score > 100 ? 100 : s.Score);
        }
        if (month.Count > 0)
        {
            return Math.Round(score / month.Count, 2);
           
        }
        return 0;
    }

    private Object getItemParam(int itemid,int weekindex, IList<MoralStuScore> scores)
    {
        DateTime timeStart = new DateTime();//开始时间
        DateTime tiemEnd = new DateTime();
        SchoolCommon.GetWeekInTerm(rYear, rTerm, SchoolMark, (weekindex + 1), out timeStart, out tiemEnd);
        foreach(MoralStuScore s in scores)
        {
            DateTime stime = (DateTime)s.StartTime;
            if (itemid == s.ItemID && stime.Year == timeStart.Year && stime.Month == timeStart.Month && stime.Day == timeStart.Day)
            {

                return s.Param;
            }
        }
        return null;
    }

    private double getWeekScore(int weekindex, IList<MoralStuScore> scores)
    {
        DateTime timeStart = new DateTime();//开始时间
        DateTime tiemEnd = new DateTime();
        SchoolCommon.GetWeekInTerm(rYear, rTerm, SchoolMark, (weekindex + 1), out timeStart, out tiemEnd);
        double score = 0;
        foreach (MoralStuScore s in scores)
        {
            DateTime stime = (DateTime)s.StartTime;
            if (stime.Year == timeStart.Year && stime.Month == timeStart.Month && stime.Day == timeStart.Day)
            {
                score+=(double) s.Score;
            }
        }
        return score;
    }

    private double getMonthScore(DateTime mtime, IList<MoralStuScore> scores)
    {
        double score = 0;
        foreach (MoralStuScore s in scores)
        {
            DateTime stime = (DateTime)s.StartTime;
            if (mtime.Month == stime.Month && mtime.Year == stime.Year)
            {
                score += (double)s.Score;
            }
        }
        return score;
    }

    private int getRolspan(int weeks,int index,int month) 
    {
        int span = 0;
        for (int state = 0; state <= weeks; state++)
        {
            if (state >= index) 
            {
                DateTime timeStart = new DateTime();//开始时间
                DateTime tiemEnd = new DateTime();
                SchoolCommon.GetWeekInTerm(rYear, rTerm, SchoolMark, (state + 1), out timeStart, out tiemEnd);
                if (month == timeStart.Month) 
                {
                    span++;
                }
            }
        }
        return span;
    }
    private DateTime getEndTime()
    {
        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", rYear);
        ht.Add("Term", rTerm);
        ht.Add("SchoolMark", SchoolMark);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList[0].EndDate != null)
            return (System.DateTime)infoList[0].EndDate;
        if (rTerm == 1)
        {
            ht = new Hashtable();
            ht.Add("SchoolYear", rYear);
            ht.Add("Term", rTerm+1);
            ht.Add("SchoolMark", SchoolMark);
        }
        else
        {
            ht = new Hashtable();
            ht.Add("SchoolYear", rYear+1);
            ht.Add("Term",  1);
            ht.Add("SchoolMark", SchoolMark);
        }

        infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList != null && infoList.Count > 0)
        {
            return (System.DateTime)infoList[0].StartDate;
        }
        return DateTime.Now;
        
    }
}
